package com.dataseek.iot.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataseek.iot.device.entity.DayUse;
import com.dataseek.iot.device.entity.DeviceAttrValue;
import com.dataseek.iot.device.entity.LiquidJar;
import com.dataseek.iot.device.mapper.LiquidJarMapper;
import com.dataseek.iot.device.service.DayUseService;
import com.dataseek.iot.device.service.DeviceAttValueService;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apiguardian.api.API;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/dayUse")
public class DayUseController {

    @Autowired
    private DayUseService dayUseService;

    @Autowired
    private LiquidJarMapper liquidJarMapper;

    @Autowired
    private DeviceAttValueService deviceAttValueService;

    @RequestMapping(value = "/queryMonth", method = RequestMethod.GET)
    public ResponseResult queryMonth(@RequestParam(value="jarCode", required=false) String jarCode,
                                   @RequestParam(value="deviceCode", required=false) String deviceCode) throws IOException {
        List<DayUse> reList = dayUseService.queryMonth(jarCode, deviceCode);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",reList);
    }

    @RequestMapping(value = "/queryYear", method = RequestMethod.GET)
    public ResponseResult queryYear(@RequestParam(value="jarCode", required=false) String jarCode,
                                   @RequestParam(value="deviceCode", required=false) String deviceCode) throws IOException {
        List<DayUse> reList = dayUseService.queryYear(jarCode, deviceCode);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",reList);
    }

    @ApiOperation("查询储液罐液体还能用几天")
    @RequestMapping(value = "/calLeftDays", method = RequestMethod.GET)
    public ResponseResult calLeftDays(@RequestParam(value="jarCode") String jarCode,
                                      @RequestParam(value="deviceCode") String deviceCode) throws IOException {
        BigDecimal reVal = BigDecimal.ZERO;
        //计算一个月以来的每天使用的平均值
        List<DayUse> reList = dayUseService.queryMonth(jarCode, deviceCode);
        if (!CollectionUtils.isEmpty(reList)) {
            BigDecimal totalUsedAmount = BigDecimal.ZERO;
            for (DayUse reDto : reList) {
                totalUsedAmount = totalUsedAmount.add(reDto.getUsedAmount());
            }
            BigDecimal avgVal = totalUsedAmount.divide(new BigDecimal(reList.size()), 2, BigDecimal.ROUND_HALF_UP);
            if (avgVal.compareTo(BigDecimal.ZERO) > 0) {
                //当前液位
                List<DeviceAttrValue> tmpList = deviceAttValueService.queryDeviceAttList(deviceCode, "liquidLevel");
                if (!CollectionUtils.isEmpty(tmpList)){
                    BigDecimal curLevel = new BigDecimal(tmpList.get(0).getAttValue());
                    reVal = curLevel.divide(avgVal, 1, BigDecimal.ROUND_HALF_UP);
                }
            }

        }
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",reVal);
    }

    @ApiOperation("大屏日平均消耗")
    @RequestMapping(value = "/qryDashboardDay", method = RequestMethod.GET)
    public ResponseResult qryDashboardDay() throws IOException {
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", dayUseService.qryDashboardDay());
    }


    @ApiOperation("大屏周消耗")
    @RequestMapping(value = "/qryDashboardWeek", method = RequestMethod.GET)
    public ResponseResult qryDashboardWeek() throws IOException {
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", dayUseService.qryDashboardWeek());
    }

    @ApiOperation("大屏月消耗")
    @RequestMapping(value = "/qryDashboardMonth", method = RequestMethod.GET)
    public ResponseResult qryDashboardMonth() throws IOException {
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", dayUseService.qryDashboardMonth());
    }

    @ApiOperation("大屏年消耗")
    @RequestMapping(value = "/qryDashboardYear", method = RequestMethod.GET)
    public ResponseResult qryDashboardYear() throws IOException {
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", dayUseService.qryDashboardYear());
    }

    @ApiOperation("大屏主页周消耗，多个储液罐返回")
    @RequestMapping(value = "/qryDashboardWeekWithJar", method = RequestMethod.GET)
    public ResponseResult qryDashboardWeekWithJar() throws IOException {
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", dayUseService.qryDashboardWeekWithJar());
    }


}
